package com.bjtu.lms.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import com.bjtu.lms.dao.BookDao;
import com.bjtu.lms.dao.BorrowDao;
import com.bjtu.lms.dao.BorrowHistoryDao;
import com.bjtu.lms.dao.SettingDao;
import com.bjtu.lms.dao.UserDao;
import com.bjtu.lms.model.Book;
import com.bjtu.lms.model.Borrow;
import com.bjtu.lms.model.Setting;
import com.bjtu.lms.model.User;
import com.bjtu.lms.util.notice.mail.MailOperation;

/**
 * 
 * 每个24小时检测是否有过期有过期 如果有过期,则通知 超过30天 更改用户状态
 * 
 * @author yilee
 * 
 */
public class CheckExpiredTask extends TimerTask {

	private BorrowDao borrowDao;
	private BorrowHistoryDao borrowHistoryDao;
	private SettingDao settingDao;
	private UserDao userDao;
	private BookDao bookDao;

	public BookDao getBookDao() {
		return bookDao;
	}

	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setSettingDao(SettingDao settingDao) {
		this.settingDao = settingDao;
	}

	public SettingDao getSettingDao() {
		return settingDao;
	}

	public BorrowDao getBorrowDao() {
		return borrowDao;
	}

	public void setBorrowDao(BorrowDao borrowDao) {
		this.borrowDao = borrowDao;
	}

	public BorrowHistoryDao getBorrowHistoryDao() {
		return borrowHistoryDao;
	}

	public void setBorrowHistoryDao(BorrowHistoryDao borrowHistoryDao) {
		this.borrowHistoryDao = borrowHistoryDao;
	}

	@Override
	public void run() {
		System.out.println("开始检测图书列表.....");
		List<Borrow> bl = borrowDao.getBorrowList();
		int borrowDays;
		for (Borrow borrow : bl) {
			borrowDays = getBorrowDays(borrow);
			System.out.println("borrowDays=" + borrowDays);
			caseDeal(borrowDays, borrow);
		}
	}

	// 得到借阅天数
	private int getBorrowDays(Borrow borrow) {
		Date borrowDate = borrow.getBorrowDate();
		long l = (new Date()).getTime() - borrowDate.getTime(); // 毫秒
		return (int) (l / 1000 / 3600 / 24);
	}

	// 各种情况处理
	// TODO
	// 钱
	private void caseDeal(int borrowDays, Borrow borrow) {
		Setting setting = settingDao.getSetting();
		int maxDays = setting.getMaxDays();
		int days = borrowDays - maxDays;
		// 到期前"1"天提醒
		if (days == -1) {
			// 到期提醒
			User u = userDao.findUserById(borrow.getUserId());
			Book b = bookDao.findBooksById(borrow.getBookId());
			System.out.println("开始提醒:用户" + u.getName() + "书籍" + b.getName()
					+ "距离到期还有1天");
			List<String> mailList = new ArrayList<String>();
			mailList.add(u.getEmail());
			String title = "【交大图书馆图书到期提醒】";
			String content = u.getName() + "同学，您借的《" + b.getName()
					+ "》还有一天到期，请按时归还 逾期则每天扣除图书价格的1%...";
			MailOperation.sendMails(title, content, mailList);
		} else if (days > 0 && days < 30) {
			// 扣钱
			User u = userDao.findUserById(borrow.getUserId());
			Book b = bookDao.findBooksById(borrow.getBookId());
			if (u.getUserType().getType().equals("正常")) {
				System.out.println("开始扣钱:用户" + u.getName() + "书籍" + b.getName()
						+ "扣除" + b.getPrice() * 0.01);
				u.getUserType().setFine(
						u.getUserType().getFine() + b.getPrice() * 0.01);
				userDao.modifyUser(u);
			} else {
				System.out.println("用户已经被锁定,不再扣钱");
			}

		} else if (days >= 30) { // 封锁账户
			User u = userDao.findUserById(borrow.getUserId());
			Book b = bookDao.findBooksById(borrow.getBookId());
			System.out.println("fuck,超过30天,用户:" + u.getName() + "锁定,钱停在"
					+ b.getPrice() * 0.01 * 30);
			u.getUserType().setType("禁用");
			userDao.modifyUser(u);
			List<String> mailList = new ArrayList<String>();
			mailList.add(u.getEmail());
			String title = "【交大图书馆图书用户账户封禁】";
			String content = u.getName() + "同学，由于超过60天明日又还书，账号已经封禁，期间不能再借书";
			MailOperation.sendMails(title, content, mailList);
		}

	}

}
